package com.etc.controller;

import com.etc.dao.CinemaDao;
import com.etc.dao.Movie_cinemaDao;
import com.etc.entity.Cinema;
import com.etc.service.CinemaService;
import com.etc.util.Msg;
import com.etc.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2019/5/21.
 */
@Controller
@RequestMapping("cinema")
public class CinemaController {
    @Autowired
    CinemaService cinemaService;
    @Autowired
    Movie_cinemaDao movie_cinemaDao;
    @Autowired
    CinemaDao cinemaDao;

    @PostMapping(value = "addcinema")
    @ResponseBody
    public Msg<String> addcinema(@RequestBody Cinema cinema) {

        Msg msg = new Msg();
        List <Cinema> list=cinemaService.getcinema(cinema.getCname());
        if (list.size()>0){

            msg.setMsg("影厅名已存在");
        }else{
            Cinema cinema1 = cinemaDao.save(cinema);
            if (cinema1 != null) {
                msg.setMsg("添加成功");
            } else {
                msg.setMsg("添加失败");
            }
        }
        return msg;
    }

    @PutMapping(value = "updatecinema")
    @ResponseBody
    public Msg<String> updatecinema(@RequestBody Cinema cinema) {
        Msg msg = new Msg();
        Cinema cinema1 = cinemaService.addcinema(cinema);
        System.out.println(cinema);
        if (cinema1 != null) {
            msg.setMsg("修改成功");
        } else {
            msg.setMsg("修改失败");
        }
        return msg;
    }
    @DeleteMapping(value = "delcinema/{cid}")
    @ResponseBody
    public Msg<String> delcinema(@PathVariable("cid") Integer cid) {
        Msg msg = new Msg();
        cinemaService.deletecinema(cid);
        msg.setMsg("删除成功");
        return msg;

    }

    @RequestMapping(value = "getcinemaName/{cname}")
    public String getcinemaName(@PathVariable("cname") String cname){

        movie_cinemaDao.findByCname(cname);
        return null;
    }

    @GetMapping(value = "cinemalist")
    @ResponseBody
    public PageData<Cinema> getcinemas(HttpServletRequest request, @RequestParam(value = "page") Integer page, @RequestParam(value = "limit") Integer limit, @RequestParam(value = "content", required = false) String content) {
        List<Cinema> list =cinemaService.findAllcname();
        request.getSession().setAttribute("pd", list);
        if (content == null) {
            content = "";
        }
        PageData<Cinema> pd = cinemaService.getCinemaByPage(page, limit, content);
        pd.setCode(0);
        return pd;
    }

}
